﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static List<string> Logs = new List<string>();

        public static void Log(params string[] msg) {
            try
            {
                if (null != msg)
                {
                    Logs.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\t" + string.Join(",", msg));
                }
                if (Logs.Count > 1100)
                {
                    Logs.RemoveRange(0, 100);
                }
            }
            catch// (Exception ex)
            {
                //
            }

        }


        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    "Biz",
            //    "Biz/{controller}/{action}/{id}",
            //    new { controller = "Hospitals",  action = "Index", id = UrlParameter.Optional });
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home",BON="none" ,action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
        protected void Application_Start()
        {
            WebUI.Services.SettingsInitializer.InitSettings();

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            using (WebUI.Models.SiteDb db = new Models.SiteDb())
            {
                var hs = db.SiteCfgs.FirstOrDefault(v => v.ID == "SmsSentHour").Value;
                int hour = 20;
                int.TryParse(hs, out hour);
                Services.SmsSessionSvc.Instance.StartTimers(hour);
            }

        }

        protected void Application_End()
        {
            Services.SmsSessionSvc.Instance.StopTimers();
            Services.SmsSessionSvc.Instance.Dispose();
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.ContentLength > 1024*1024*10)
            {
                Response.Write("<script type='text/javascript'>alert('图片太过巨大，无法处理。')</script>");
                Response.Flush();
                Response.End();
            }
        }  
    }
}